<?php

//UPDATE abstractions
//used by users and activegames only
	function updateSingleField($tablename,$whereKey,$whereValue,$updateKey,$updateValue){
		$updateValue = "'$updateValue'";
		$whereValue = "'$whereValue'";
		$query="UPDATE `$tablename` " .
			"SET `$updateKey`=$updateValue " .
			"WHERE `$whereKey`=$whereValue LIMIT 1;";
		$result = mysql_query($query);
		if (!$result) {
			return false;
		} else {
			return true;
		}
	}
	
	//update a setting in settings table
	function updateSettings($name,$value){
		global $settings;
		$settings[$name]=$value;
		return updateSingleField(table_settings,"name",$name,"value",$value);
	}

//SELECT abstractions
	function select($what,$table,$whereKey,$whereVal,$otherConds=""){
		if ($otherConds != null){$otherConds = " " . $otherConds;}
		
		if ($whereKey==null || $whereVal==null){
			$where=null;
		} else {
			if(!is_array($whereVal)){
				$whereVal="'$whereVal'";
				$where = "WHERE `$whereKey`=$whereVal";
			} else {
				foreach($whereVal as &$val){
					$newVals[]="'$val'";
				}
				$whereins=implode(",",$newVals);
				$where = "WHERE `$whereKey` IN ($whereins)";
			}
		}
		$query = "SELECT $what from `$table` $where $otherConds;";
		$result = mysql_query($query);
		if(isset($result) && $result){
			while($row=mysql_fetch_array($result)){
				$rows[]=$row;
			}
		}
		if (isset($rows) && $rows){
			return $rows;	
		} else {
			return false;
		}
	}



	
//DELETE abstractions
	function delete($table,$whereKey,$whereVal){
		$whereVal = "'$whereVal'";
		$query = "DELETE FROM `$table` WHERE `$whereKey`=$whereVal;";
		$result = mysql_query($query);
		if (!$result){
			return false;
		} else {
			return true;
		}
	}

	function selectSingleFromSettings($what,$whereKey,$whereVal,$otherConds=""){
		$result = select($what,table_settings,$whereKey,$whereVal,$otherConds);
		return $result[0];
	}
	
	function getSettingValue($settingname){
		$result=selectSingleFromSettings("*","name",$settingname,null);
		return $result['value'];
	}
	
	function insertNewSetting($name,$value){
		global $settings;
		$settings[$name]=$value;
		$query="INSERT INTO `".table_settings."` ".
				"(`name`, `value`) ".
				"VALUES ('$name', '$value');";
		$result=mysql_query($query);
		return $result;
	}
	
//load global settings stored in database
	function loadGlobalSettings(){
		$settings=null;
		$result=select("*",table_settings,null,null,null);
		if($result){
			foreach($result as $row){
				$settings[$row['name']]=$row['value'];
			}
		}
		return $settings;
	}
	
//load global settings of the site from database
//requires database.php
//load the $settings array object
$settings=loadGlobalSettings();

?>